package com.muqi.app.network.http;

import java.util.HashMap;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.muqi.app.afinalcache.ACache;
import com.muqi.app.loadingview.SVProgressHUD;
import com.muqi.app.project.application.ActivitiesStack;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.LogUtil;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.login.LoginActivity;
import com.muqi.app.qlearn.student.R;

public class MyAsyncHttp {
	
	private static ACache mCache = null;
	
	public enum RESULTCODE{
		FAILURE,
		ERROR ,
		LOCAL ,
		SUCCESS ,
		NONET ,
		PARSEERROR
		}
	
	private static AsyncHttpClient client = new AsyncHttpClient();// 实例对象
	private static SVProgressHUD mSVProgressHUD;

	static { 
        client.setTimeout(20000); // 设置链接超时，如果不设置，默认为10s
        client.setMaxRetriesAndTimeout(3, 60000);//重连次数和超时
    }
	
	public interface AsyncCallBackListener {
		void onAsyncCallBack(RESULTCODE resuleCode, String request, String result);
	}
	public interface AsyncWithProgressListener {
		void onAsyncCallBack(RESULTCODE resuleCode, String request, String result);
		void onProgress(int bytesWritten, int totalSize);
	}
	
	/**
	 * 
	 * @return
	 */
	public static AsyncHttpClient getClient() {
		return client;
	}
	
	private static String getAbsoluteUrl(String relativeUrl) {
		String RequestUrl = NetWorkApi.SERVER_IP + relativeUrl;
		LogUtil.e("发送的请求"+RequestUrl);
		return RequestUrl;
	}
	
	public static void cancelRequest(Context mContext){
		client.cancelRequests(mContext, true);
	}
	
	public static void cancelAllRequest(){
		client.cancelAllRequests(true);
	}
	
	/*********************************Get方法*************************************************/
	
	/**
	 * 先获取首页缓存再加载网络数据
	 * @param mContext
	 * @param urlString
	 * @param mListener
	 * @param page
	 */
	
	public static void getCacheDataHttp(final Context mContext,final String urlString,final AsyncCallBackListener mListener,final int page) {
		if(mCache == null){
			mCache = ACache.get(mContext.getApplicationContext());
		}
		if (page == 1) {
			String cacheJson = mCache.getAsString(urlString);
			if (!TextUtils.isEmpty(cacheJson)) {
				if (mListener != null)
					mListener.onAsyncCallBack(RESULTCODE.LOCAL, urlString, cacheJson);//onOverResult("local"+urlString, cacheJson);
			}
		}

		if (NetWorkUtils.isNetworkAvailable(mContext)) {

			client.get(getAbsoluteUrl(urlString),new BaseJsonHttpResponseHandler<JSONObject>() {


						@Override

						public void onFailure(int arg0, Header[] arg1,Throwable arg2, String arg3, JSONObject arg4) {

							ShowToast.showShort(mContext, R.string.sever_break);
							if (mListener != null)
								mListener.onAsyncCallBack(RESULTCODE.FAILURE,urlString, NetWorkApi.REQUEST_FAILED);
						}

						@Override

						public void onSuccess(int arg0, Header[] arg1,String result, JSONObject arg3) {
							LogUtil.e("返回的结果为："+result);
							try {
								JSONObject resultAllata = new JSONObject(result.toString());
								int errcode = resultAllata.getInt("errcode");
								String errdesc = resultAllata.getString("errdesc");
								if (errcode == 0) {
									mListener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, result.toString());
								} else if(errcode == 90000){
									ShowToast.showShort(mContext, errdesc);
									Intent intent = new Intent();
									intent.setClass(mContext, LoginActivity.class);
									mContext.startActivity(intent);
									ActivitiesStack.getInstance().popAll(true);
								} else {
									ShowToast.showShort(mContext, errdesc);
									mListener.onAsyncCallBack(RESULTCODE.ERROR,urlString, errdesc);
								}
							} catch (Exception e) {
								mListener.onAsyncCallBack(RESULTCODE.ERROR,urlString, NetWorkApi.REQUEST_JSON_FAILED);
							}
							
//							LogUtil.e("返回的json为：" + result);
//							if (mListener != null)
//								mListener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, result.toString());
//							if (mCache != null && page == 1)
//								mCache.put(urlString, result,ACache.TIME_DAY * 3);

						}

						@Override
						protected JSONObject parseResponse(String arg0,
								boolean arg1) throws Throwable {
							return null;
						}
					});
		} else {
			ShowToast.showShort(mContext, R.string.net_break);
		}
	}
	/**
	 * 先获取首页缓存再加载网络数据
	 * @param mContext
	 * @param urlString
	 * @param mListener
	 * @param page
	 */
	
	public static void getCacheDataAbsolutelyHttp(final Context mContext,final String urlString,final AsyncCallBackListener mListener,final int page) {
		if(mCache == null){
			mCache = ACache.get(mContext.getApplicationContext());
		}
		if (page == 1) {
			String cacheJson = mCache.getAsString(urlString);
			if (!TextUtils.isEmpty(cacheJson)) {
				if (mListener != null)
					mListener.onAsyncCallBack(RESULTCODE.LOCAL, urlString, cacheJson);//onOverResult("local"+urlString, cacheJson);
			}
		}

		if (NetWorkUtils.isNetworkAvailable(mContext)) {

			client.get(urlString,new BaseJsonHttpResponseHandler<JSONObject>() {


						@Override

						public void onFailure(int arg0, Header[] arg1,Throwable arg2, String arg3, JSONObject arg4) {

							ShowToast.showShort(mContext, R.string.sever_break);
							if (mListener != null)
								mListener.onAsyncCallBack(RESULTCODE.FAILURE,urlString, NetWorkApi.REQUEST_FAILED);
						}

						@Override

						public void onSuccess(int arg0, Header[] arg1,String result, JSONObject arg3) {
							LogUtil.e("返回的结果为："+result);
							try {
								JSONObject resultAllata = new JSONObject(result.toString());
								int errcode = resultAllata.getInt("errcode");
								String errdesc = resultAllata.getString("errdesc");
								if (errcode == 0) {
									mListener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, result.toString());
								} else if(errcode == 90000){
									ShowToast.showShort(mContext, errdesc);
									Intent intent = new Intent();
									intent.setClass(mContext, LoginActivity.class);
									mContext.startActivity(intent);
									ActivitiesStack.getInstance().popAll(true);
								} else {
									ShowToast.showShort(mContext, errdesc);
									mListener.onAsyncCallBack(RESULTCODE.ERROR,urlString, errdesc);
								}
							} catch (Exception e) {
								mListener.onAsyncCallBack(RESULTCODE.ERROR,urlString, NetWorkApi.REQUEST_JSON_FAILED);
							}
							
//							LogUtil.e("返回的json为：" + result);
//							if (mListener != null)
//								mListener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, result.toString());
//							if (mCache != null && page == 1)
//								mCache.put(urlString, result,ACache.TIME_DAY * 3);

						}

						@Override
						protected JSONObject parseResponse(String arg0,
								boolean arg1) throws Throwable {
							return null;
						}
					});
		} else {
			ShowToast.showShort(mContext, R.string.net_break);
		}
	}
	
	
	/**
	 * 
	 * @param context 请求传入context，在destroy时取消未完成请求，避免僵尸请求
	 * @param reqMap
	 * @param urlString
	 * @param listener
	 */
	public static void get(final Context context, HashMap<String, Object> reqMap, final String urlString, final AsyncCallBackListener listener){
		if(NetWorkUtils.isNetworkAvailable(context)){
			client.get(context, getAbsoluteUrl(ParamsUtils.buildParams(urlString, reqMap)), new TextHttpResponseHandler() {
				
				@Override
				public void onStart() {
					super.onStart();
				}
				
				@Override
				public void onProgress(int bytesWritten, int totalSize) {
					super.onProgress(bytesWritten, totalSize);
				}

				@Override
				public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
					ShowToast.showShort(context, NetWorkApi.REQUEST_FAILED);
					listener.onAsyncCallBack(RESULTCODE.FAILURE, urlString, NetWorkApi.REQUEST_FAILED);
				}

				@Override
				public void onSuccess(int arg0, Header[] arg1, String arg2) {
					LogUtil.e("返回的结果为："+arg2);
					try {
						JSONObject resultAllata = new JSONObject(arg2.toString());
						int errcode = resultAllata.getInt("errcode");
						String errdesc = resultAllata.getString("errdesc");
						if (errcode == 0) {
							listener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, arg2.toString());
						} else if(errcode == 90000){
							ShowToast.showShort(context, errdesc);
							Intent intent = new Intent();
							intent.setClass(context, LoginActivity.class);
							context.startActivity(intent);
							ActivitiesStack.getInstance().popAll(true);
						} else {
							ShowToast.showShort(context, errdesc);
							listener.onAsyncCallBack(RESULTCODE.ERROR,urlString, "");
						}
					} catch (Exception e) {
						listener.onAsyncCallBack(RESULTCODE.ERROR,urlString, NetWorkApi.REQUEST_JSON_FAILED);
					}
				}
				
				@Override
				public void onFinish() {
					super.onFinish();
				}
				
				@Override
				public void onRetry(int retryNo) {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"重连次数——"+retryNo);
					super.onRetry(retryNo);
				}
				
				@Override
				public void onCancel() {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"取消");
					super.onCancel();
				}
			});
		}else{
			ShowToast.showShort(context, R.string.net_break);

			listener.onAsyncCallBack(RESULTCODE.NONET,urlString, NetWorkApi.REQUEST_NETWORK_BREAKED);
		}
	}
	
	public static void getSpecialData(final Context context, final String urlString, final AsyncCallBackListener listener){
		if(NetWorkUtils.isNetworkAvailable(context)){
			client.get(context, getAbsoluteUrl(urlString), new TextHttpResponseHandler() {
				
				@Override
				public void onStart() {
					super.onStart();
				}
				
				@Override
				public void onProgress(int bytesWritten, int totalSize) {
					super.onProgress(bytesWritten, totalSize);
				}

				@Override
				public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
					listener.onAsyncCallBack(RESULTCODE.FAILURE, urlString, NetWorkApi.REQUEST_FAILED);
				}

				@Override
				public void onSuccess(int arg0, Header[] arg1, String arg2) {
//					try {
//						JSONObject resultAllata = new JSONObject(arg2.toString());
//						int errcode = resultAllata.getInt("errcode");
//						String errdesc = resultAllata.getString("errdesc");
//						if (errcode == 0) {
							listener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, arg2.toString());
//						} else if(errcode == 90000){
//							ShowToast.showShort(context, errdesc);
//							Intent intent = new Intent();
//							intent.setClass(context, LoginActivity.class);
//							context.startActivity(intent);
//							ActivitiesStack.getInstance().popAll(true);
//						} else {
//							ShowToast.showShort(context, errdesc);
//							listener.onAsyncCallBack(RESULTCODE.ERROR, urlString, "");
//						}
//					} catch (Exception e) {
//						listener.onAsyncCallBack(RESULTCODE.PARSEERROR,urlString, NetWorkApi.REQUEST_JSON_FAILED);
//					}
				}
				
				@Override
				public void onFinish() {
					super.onFinish();
				}
				
				@Override
				public void onRetry(int retryNo) {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"重连次数——"+retryNo);
					super.onRetry(retryNo);
				}
				
				@Override
				public void onCancel() {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"取消");
					super.onCancel();
				}
			});
		}else{
			ShowToast.showShort(context, R.string.net_break);
			listener.onAsyncCallBack(RESULTCODE.NONET, urlString, NetWorkApi.REQUEST_NETWORK_BREAKED);
		}
	}
	
	
	/**
	 * 
	 * @param context 请求传入context，在destroy时取消未完成请求，避免僵尸请求
	 * @param urlString 
	 * @param listener 回调
	 */
	public static void get(final Context context, final String urlString, final AsyncCallBackListener listener){
		if(NetWorkUtils.isNetworkAvailable(context)){
			client.get(context, getAbsoluteUrl(urlString), new TextHttpResponseHandler() {
				
				@Override
				public void onStart() {
					super.onStart();
				}
				
				@Override
				public void onProgress(int bytesWritten, int totalSize) {
					super.onProgress(bytesWritten, totalSize);
				}

				@Override
				public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
					ShowToast.showShort(context, NetWorkApi.REQUEST_FAILED);
					listener.onAsyncCallBack(RESULTCODE.FAILURE, urlString, NetWorkApi.REQUEST_FAILED);
				}

				@Override
				public void onSuccess(int arg0, Header[] arg1, String arg2) {
					//Log.e("===", arg2);
					try {
						JSONObject resultAllata = new JSONObject(arg2.toString());
						int errcode = resultAllata.getInt("errcode");
						String errdesc = resultAllata.getString("errdesc");
						if (errcode == 0) {
							listener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, arg2.toString());
						} else if(errcode == 90000){
							ShowToast.showShort(context, errdesc);
							Intent intent = new Intent();
							intent.setClass(context, LoginActivity.class);
							context.startActivity(intent);
							ActivitiesStack.getInstance().popAll(true);
						}else if(errcode==3000){
							listener.onAsyncCallBack(RESULTCODE.NONET, urlString, errdesc);
						} else {
							//ShowToast.showShort(context, errdesc);
							listener.onAsyncCallBack(RESULTCODE.ERROR, urlString, errdesc);
						}
					} catch (JSONException e) {
						listener.onAsyncCallBack(RESULTCODE.PARSEERROR,urlString, NetWorkApi.REQUEST_JSON_FAILED);
					}
				}
				
				@Override
				public void onFinish() {
					super.onFinish();
				}
				
				@Override
				public void onRetry(int retryNo) {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"重连次数——"+retryNo);
					super.onRetry(retryNo);
				}
				
				@Override
				public void onCancel() {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"取消");
					super.onCancel();
				}
			});
		}else{
			ShowToast.showShort(context, R.string.net_break);
			listener.onAsyncCallBack(RESULTCODE.NONET, urlString, NetWorkApi.REQUEST_NETWORK_BREAKED);
		}
	}
	/**
	 * 有加载动画的get请求，可用于同步等待下载或长时请求
	 * @param context
	 * @param urlString
	 * @param listener
	 */
	public static void getWithUI(final Context context, final String urlString, final AsyncCallBackListener listener){
		if(NetWorkUtils.isNetworkAvailable(context)){
			client.get(context, getAbsoluteUrl(urlString), new TextHttpResponseHandler() {
				
				@Override
				public void onStart() {
					if(mSVProgressHUD == null){
						mSVProgressHUD = new SVProgressHUD(context);
					}
					mSVProgressHUD.showWithStatus("数据加载中...", SVProgressHUD.SVProgressHUDMaskType.Gradient);
					super.onStart();
				}
				
				@Override
				public void onProgress(int bytesWritten, int totalSize) {
					super.onProgress(bytesWritten, totalSize);
				}

				@Override
				public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
					ShowToast.showShort(context, NetWorkApi.REQUEST_FAILED);
					listener.onAsyncCallBack(RESULTCODE.FAILURE, urlString, NetWorkApi.REQUEST_FAILED);
				}

				@Override
				public void onSuccess(int arg0, Header[] arg1, String arg2) {
					try {
						JSONObject resultAllata = new JSONObject(arg2.toString());
						int errcode = resultAllata.getInt("errcode");
						String errdesc = resultAllata.getString("errdesc");
						if (errcode == 0) {
							listener.onAsyncCallBack(RESULTCODE.SUCCESS, urlString, arg2.toString());
						} else if(errcode == 90000){
							ShowToast.showShort(context, errdesc);
							Intent intent = new Intent();
							intent.setClass(context, LoginActivity.class);
							context.startActivity(intent);
							ActivitiesStack.getInstance().popAll(true);
						} else {
							ShowToast.showShort(context, errdesc);
							listener.onAsyncCallBack(RESULTCODE.ERROR,urlString, "");
						}
					} catch (Exception e) {
						listener.onAsyncCallBack(RESULTCODE.PARSEERROR,urlString, NetWorkApi.REQUEST_JSON_FAILED);
					}
				}
				
				@Override
				public void onFinish() {
					if(mSVProgressHUD!=null && mSVProgressHUD.isShowing()){
						mSVProgressHUD.dismissImmediately();
						mSVProgressHUD = null;
					}
					super.onFinish();
				}
				
				@Override
				public void onRetry(int retryNo) {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"重连次数——"+retryNo);
					super.onRetry(retryNo);
				}
				
				@Override
				public void onCancel() {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"取消");
					super.onCancel();
				}
			});
		}else{
			ShowToast.showShort(context, R.string.net_break);
			listener.onAsyncCallBack(RESULTCODE.NONET,urlString, NetWorkApi.REQUEST_NETWORK_BREAKED);
		}
	}
	
	/*********************************上传头像*************************************************/
	
	public static void postAvatar(final Context context, RequestParams params, final String urlString, final AsyncCallBackListener listener){
		if(NetWorkUtils.isNetworkAvailable(context)){
			client.post(context, getAbsoluteUrl(urlString), params, new TextHttpResponseHandler() {
				
				@Override
				public void onStart() {
					if(mSVProgressHUD == null){
						mSVProgressHUD = new SVProgressHUD(context);
					}
					mSVProgressHUD.getProgressBar().setProgress(0);
				    mSVProgressHUD.showWithProgress("进度 " + 0 + "%", SVProgressHUD.SVProgressHUDMaskType.Gradient);
					super.onStart();
				}
				
				@Override
				public void onProgress(int bytesWritten, int totalSize) {
					int progress = bytesWritten*100/totalSize;
					if (mSVProgressHUD.getProgressBar().getMax() != mSVProgressHUD.getProgressBar().getProgress()){
						mSVProgressHUD.getProgressBar().setProgress(progress);
					    mSVProgressHUD.setText("进度 "+progress+"%");
					}
					super.onProgress(bytesWritten, totalSize);
				}

				@Override
				public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
					ShowToast.showShort(context, NetWorkApi.REQUEST_FAILED);
					listener.onAsyncCallBack(RESULTCODE.FAILURE, urlString, NetWorkApi.REQUEST_FAILED);
					hindProgress();
				}

				@Override
				public void onSuccess(int arg0, Header[] arg1, String arg2) {
					hindProgress();
					listener.onAsyncCallBack(RESULTCODE.SUCCESS, urlString, arg2.toString());
				}
				
				@Override
				public void onFinish() {
					hindProgress();
					super.onFinish();
				}
				
				@Override
				public void onRetry(int retryNo) {
					hindProgress();
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"重连次数——"+retryNo);
					super.onRetry(retryNo);
				}
				
				@Override
				public void onCancel() {
					hindProgress();
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"取消");
					super.onCancel();
				}
				
				private void hindProgress(){
					if(mSVProgressHUD!=null && mSVProgressHUD.isShowing()){
						mSVProgressHUD.dismissImmediately();
						mSVProgressHUD = null;
					}
				}
			});
		}else{
			ShowToast.showShort(context, R.string.net_break);
			listener.onAsyncCallBack(RESULTCODE.NONET,urlString, NetWorkApi.REQUEST_NETWORK_BREAKED);
		}
	}
	/*********************************Post方法*************************************************/
	
	public static void post(final Context context, RequestParams params, final String urlString, final AsyncCallBackListener listener){
		if(NetWorkUtils.isNetworkAvailable(context)){
			client.post(context, getAbsoluteUrl(urlString), params, new TextHttpResponseHandler() {
				
				@Override
				public void onStart() {
					if(mSVProgressHUD == null){
						mSVProgressHUD = new SVProgressHUD(context);
					}
					mSVProgressHUD.getProgressBar().setProgress(0);
				    mSVProgressHUD.showWithProgress("进度 " + 0 + "%", SVProgressHUD.SVProgressHUDMaskType.Gradient);
					super.onStart();
				}
				
				@Override
				public void onProgress(int bytesWritten, int totalSize) {
					int progress = bytesWritten*100/totalSize;
					if (mSVProgressHUD.getProgressBar().getMax() != mSVProgressHUD.getProgressBar().getProgress()){
						mSVProgressHUD.getProgressBar().setProgress(progress);
					    mSVProgressHUD.setText("进度 "+progress+"%");
					}
					super.onProgress(bytesWritten, totalSize);
				}

				@Override
				public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
					ShowToast.showShort(context, NetWorkApi.REQUEST_FAILED);
					listener.onAsyncCallBack(RESULTCODE.FAILURE, urlString, NetWorkApi.REQUEST_FAILED);
					hindProgress();
				}

				@Override
				public void onSuccess(int arg0, Header[] arg1, String arg2) {
					hindProgress();
					try {
						JSONObject resultAllata = new JSONObject(arg2.toString());
						int errcode = resultAllata.getInt("errcode");
						String errdesc = resultAllata.getString("errdesc");
						if (errcode == 0) {
							listener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, arg2.toString());
						} else if(errcode == 90000){
							ShowToast.showShort(context, errdesc);
							Intent intent = new Intent();
							intent.setClass(context, LoginActivity.class);
							context.startActivity(intent);
							ActivitiesStack.getInstance().popAll(true);
						} else {
							ShowToast.showShort(context, errdesc);
							listener.onAsyncCallBack(RESULTCODE.ERROR,urlString,"");
						}
					} catch (Exception e) {

						listener.onAsyncCallBack(RESULTCODE.PARSEERROR,urlString, NetWorkApi.REQUEST_JSON_FAILED);

					}
				}
				
				@Override
				public void onFinish() {
					hindProgress();
					super.onFinish();
				}
				
				@Override
				public void onRetry(int retryNo) {
					hindProgress();
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"重连次数——"+retryNo);
					super.onRetry(retryNo);
				}
				
				@Override
				public void onCancel() {
					hindProgress();
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"取消");
					super.onCancel();
				}
				
				private void hindProgress(){
					if(mSVProgressHUD!=null && mSVProgressHUD.isShowing()){
						mSVProgressHUD.dismissImmediately();
						mSVProgressHUD = null;
					}
				}
			});
		}else{
			ShowToast.showShort(context, R.string.net_break);
			listener.onAsyncCallBack(RESULTCODE.NONET,urlString, NetWorkApi.REQUEST_NETWORK_BREAKED);
		}
	}
	
	public static void postWithProgress(final Context context, RequestParams params, final String urlString, final AsyncWithProgressListener listener){
		if(NetWorkUtils.isNetworkAvailable(context)){
			client.post(context, getAbsoluteUrl(urlString), params, new TextHttpResponseHandler() {
				
				@Override
				public void onStart() {
					super.onStart();
				}
				
				@Override
				public void onProgress(int bytesWritten, int totalSize) {
					listener.onProgress(bytesWritten, totalSize);
					super.onProgress(bytesWritten, totalSize);
				}

				@Override
				public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
					ShowToast.showShort(context, NetWorkApi.REQUEST_FAILED);
					listener.onAsyncCallBack(RESULTCODE.FAILURE, urlString, NetWorkApi.REQUEST_FAILED);
				}

				@Override
				public void onSuccess(int arg0, Header[] arg1, String arg2) {
					try {
						JSONObject resultAllata = new JSONObject(arg2.toString());
						int errcode = resultAllata.getInt("errcode");
						String errdesc = resultAllata.getString("errdesc");
						if (errcode == 0) {
							listener.onAsyncCallBack(RESULTCODE.SUCCESS,urlString, arg2.toString());
						} else {
							ShowToast.showShort(context, errdesc);
							listener.onAsyncCallBack(RESULTCODE.ERROR, urlString, "");
						}
					} catch (Exception e) {
						listener.onAsyncCallBack(RESULTCODE.PARSEERROR, urlString, NetWorkApi.REQUEST_JSON_FAILED);
					}
				}
				
				@Override
				public void onFinish() {
					super.onFinish();
				}
				
				@Override
				public void onRetry(int retryNo) {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"重连次数——"+retryNo);
					super.onRetry(retryNo);
				}
				
				@Override
				public void onCancel() {
					LogUtil.e("请求"+getAbsoluteUrl(urlString)+"取消");
					super.onCancel();
				}
			});
		}else{
			ShowToast.showShort(context, R.string.net_break);
			listener.onAsyncCallBack(RESULTCODE.NONET, urlString, NetWorkApi.REQUEST_NETWORK_BREAKED);
		}
	}
}
